package com.kede.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kede.dto.AjaxMessage;
import com.kede.dto.LayuiTableData;
import com.kede.entity.Product;
import com.kede.entity.Role;
import com.kede.entity.User;
import com.kede.service.UserRoleService;
import com.kede.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 【请填写作者】
 * @since 2020-04-09
 * @since 2020-04-11
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    UserRoleService userRoleService;
    /**
     * 个人信息修改页面跳转
     * @param model
     * @return
     */
    @GetMapping("information")
    public String information(Model model){
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        model.addAttribute("user",user);
        return "user/information";
    }
    @ResponseBody
    @PostMapping("update")
    public AjaxMessage update(
            User user
    ){
        User user1 = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        /*获取当前时间*/
        LocalDateTime nowTime = LocalDateTime.now();
        user1.setUpdateDate(nowTime);
        user1.setNickname(user.getNickname());
        user1.setSex(user.getSex());
        user1.setPicture(user.getPicture());
        user1.setEmail(user.getEmail());
        user1.setPhone(user.getPhone());
        user1.setAddress(user.getAddress());
        user1.setBirthday(user.getBirthday());
        user1.setRemark(user.getRemark());
        AjaxMessage message = new AjaxMessage();
        if (userService.updateById(user1)){
            message.setCode(0);
            SecurityUtils.getSubject().getSession().setAttribute("user",user1);
        }else {
            message.setCode(1);
            message.setMsg("失败");
        }
        return message;
    }
    /**
     *修改密码页面跳转
     * @return
     */
    @GetMapping("editpwd")
    public String editpwd(){
        return "user/editpwd";
    }
    @ResponseBody
    @PostMapping("editpwd")
    public AjaxMessage editpwd(
            String oldpassword,
            String newpassword
    ){
        AjaxMessage message = new AjaxMessage();
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        String oldpas = DigestUtils.md5DigestAsHex(oldpassword.getBytes());
        String newpas=DigestUtils.md5DigestAsHex(newpassword.getBytes());
        if (oldpas.equals(user.getPassword())){
            user.setPassword(newpas);
            if (userService.updateById(user)){
                message.setCode(0);
            }else {
                message.setCode(1);
                message.setMsg("失败");
            }
        }else {
            message.setCode(1);
            message.setMsg("失败");
        }
        return message;
    }
    @GetMapping("list")
    public String list(Model model){
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        model.addAttribute("user",user);
        return "user/list";
    }
    @ResponseBody
    @GetMapping("userlist")
    public LayuiTableData list(
            String id,
            String name,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "limit",defaultValue = "10") Integer limit
    ){
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        if(StringUtils.hasLength(id)){
            int sum=Integer.parseInt(id);
            if (StringUtils.hasLength(name)){
                if (sum==1){
                    wrapper.like("username",name);
                }else if (sum==2){
                    wrapper.like("nickname",name);
                }else if (sum==3){
                    wrapper.like("phone",name);
                }else if (sum==4){
                    wrapper.like("email",name);
                }
            }
    }
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");

        Page<User> rolePage = new Page<>();

        rolePage.setCurrent(page);
        rolePage.setSize(limit);

        Page<User> pageRlt = userService.page(rolePage, wrapper);
        LayuiTableData tableDate = new LayuiTableData();
        List<User> users = new ArrayList<User>();
        Role userrole=userRoleService.getRoleByUid(user.getId());
        for (User user1:pageRlt.getRecords()) {
            Role role=userRoleService.getRoleByUid(user1.getId());
            if (role==null){
                System.out.print("kong");
            }
            if ( userrole.getId()<role.getId()&&role.getId()==3){
                user1.setRole(role.getTitle());
                users.add(user1);
            }
        }
        tableDate.setData(users);
        tableDate.setCount(pageRlt.getTotal());
        tableDate.setCode(0);
        return tableDate;
    }
    @GetMapping("modify")
    public String modify(
            String uid,
            Model model
    ){
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        User user1=userService.getById(uid);
        Role role=userRoleService.getRoleByUid(user.getId());
        Role role1=userRoleService.getRoleByUid(user1.getId());
        model.addAttribute("role",role);
        model.addAttribute("role1",role1);
        model.addAttribute("user1",user1);
        return "user/modify";
    }
    @ResponseBody
    @PostMapping("modify")
    public AjaxMessage modify(
        String roleid,
        String status,
        String uid
    ){
        if (StringUtils.hasLength(roleid)){
            userRoleService.setRole(roleid,uid);
        }
        User user=userService.getById(uid);
        user.setStatus(Integer.parseInt(status));
        AjaxMessage message = new AjaxMessage();
        if (userService.updateById(user)){
            message.setCode(0);
        }

        return message;
    }
    @ResponseBody
    @PostMapping("add")
    public AjaxMessage add(
            User user
    ){
        AjaxMessage message = new AjaxMessage();
        LocalDateTime nowTime = LocalDateTime.now();
        user.setUpdateDate(nowTime);
        user.setCreateDate(nowTime);
        return message;
    }
    @GetMapping("admin")
    public String admin(Model model){
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        model.addAttribute("user",user);
        return "user/admin";
    }
    @ResponseBody
    @GetMapping("adminlist")
    public LayuiTableData admin(
            String id,
            String name,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "limit",defaultValue = "10") Integer limit
    ){
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        if(StringUtils.hasLength(id)){
            int sum=Integer.parseInt(id);
            if (StringUtils.hasLength(name)){
                if (sum==1){
                    wrapper.like("username",name);
                }else if (sum==2){
                    wrapper.like("nickname",name);
                }else if (sum==3){
                    wrapper.like("phone",name);
                }else if (sum==4){
                    wrapper.like("email",name);
                }
            }
        }
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");

        Page<User> rolePage = new Page<>();

        rolePage.setCurrent(page);
        rolePage.setSize(limit);

        Page<User> pageRlt = userService.page(rolePage, wrapper);
        LayuiTableData tableDate = new LayuiTableData();
        List<User> users = new ArrayList<User>();
        Role userrole=userRoleService.getRoleByUid(user.getId());
        for (User user1:pageRlt.getRecords()) {
            Role role=userRoleService.getRoleByUid(user1.getId());
            if (role==null){
                System.out.print("kong");
            }
            if ( userrole.getId()<role.getId()&&role.getId()==2){
                user1.setRole(role.getTitle());
                users.add(user1);
            }
        }
        tableDate.setData(users);
        tableDate.setCount(pageRlt.getTotal());
        tableDate.setCode(0);
        return tableDate;
    }
}
